package com.learn.grpc.net;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.learn.grpc.grpc.NetServiceGrpc;
import com.learn.grpc.hello.PingRequest;
import com.learn.grpc.hello.PingResponse;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

@Slf4j
public class NetServiceGrpcImpl extends NetServiceGrpc.NetServiceImplBase {

    @Override
    public void normalMethod(PingRequest request, StreamObserver<PingResponse> responseObserver) {

        String content = request.getContent();
        System.out.println("请求参数:" + content);

        PingResponse response = PingResponse.newBuilder()
                .setCode(0).setMessage("success")
                .setData(content + ":" + LocalDateTime.now())
                .build();

        // 向客户端写入数据
        responseObserver.onNext(response);
        // 通知客户端，写入完成
        responseObserver.onCompleted();
    }

    @Override
    public void serverStreamMethod(PingRequest request, StreamObserver<PingResponse> responseObserver) {

        String content = request.getContent();
        System.out.println("请求参数:" + content);

        for (int i = 1; i < 11; i++) {
            PingResponse response = PingResponse.newBuilder()
                    .setCode(0).setMessage("success")
                    .setData(i + ":" + content + ":" + LocalDateTime.now())
                    .build();

            // 向客户端写入数据
            responseObserver.onNext(response);

            ThreadUtil.sleep(500);
        }

        // 通知客户端，写入完成
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<PingRequest> clientStreamMethod(StreamObserver<PingResponse> responseObserver) {

        StreamObserver<PingRequest> requestObserver = new StreamObserver<PingRequest>() {

            private String batchNo = RandomUtil.randomString(8);

            @Override
            public void onNext(PingRequest value) {
                log.info("[{}] 接收到客户端发送的数据, 请求内容:{}", batchNo, value);
            }

            @Override
            public void onError(Throwable t) {
                log.info("[{}] 客户端数据发送异常，异常原因:{}", batchNo, t.getMessage());
            }

            @Override
            public void onCompleted() {
                log.info("[{}] 客户端请求数据发送完毕", batchNo);
            }
        };

        return requestObserver;
    }

    @Override
    public StreamObserver<PingRequest> doubleStreamMethod(StreamObserver<PingResponse> responseObserver) {


        StreamObserver<PingRequest> requestObserver = new StreamObserver<PingRequest>() {

            private String batchNo = RandomUtil.randomNumbers(8);

            @Override
            public void onNext(PingRequest value) {
                log.info("[{}] 接收到客户端发送的数据, 请求内容:{}", batchNo, value);

                // 接收到请求后同时返回数据
                PingResponse pingResponse = PingResponse.newBuilder().setCode(0).setMessage(batchNo).setData("请求被接收").build();
                responseObserver.onNext(pingResponse);
            }

            @Override
            public void onError(Throwable t) {
                log.info("[{}] 客户端数据发送异常，异常原因:{}", batchNo, t.getMessage());
            }

            @Override
            public void onCompleted() {
                log.info("[{}] 客户端请求数据发送完毕", batchNo);

                // 接收到请求后同时返回数据
                PingResponse pingResponse = PingResponse.newBuilder()
                        .setCode(0).setMessage(batchNo).setData("请求被接收").build();
                responseObserver.onNext(pingResponse);

                // 通知客户端请求已处理完
                responseObserver.onCompleted();
            }
        };

        return requestObserver;
    }
}
